from rest_framework.generics import ListAPIView, ListCreateAPIView

from apps.meiduo_admin.serializers.user_serializers import UserModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.users.models import User


class UserListView(ListCreateAPIView):

    # queryset = User.objects.all()

    serializer_class = UserModelSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if not keyword:
            queryset = User.objects.all()
        else:
            queryset = User.objects.filter(username=keyword)

        return queryset

